package com.ziven.common.widget.value;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class FontFamily extends Value {

    public static FontFamily typeface(@NonNull String fontFamily, int style) {
        return new FontFamily().accept(my -> {
            my.resId = style;
            my.value = fontFamily;
        });
    }

    public static FontFamily normal(@NonNull String fontFamily) {
        return typeface(fontFamily, Typeface.NORMAL);
    }

    public static FontFamily bold(@NonNull String fontFamily) {
        return typeface(fontFamily, Typeface.BOLD);
    }

    public static FontFamily normal() {
        return style(Typeface.NORMAL);
    }

    public static FontFamily bold() {
        return style(Typeface.BOLD);
    }

    public static FontFamily style(int style) {
        return typeface(Typeface.defaultFromStyle(style));
    }

    public static FontFamily typeface(@NonNull Typeface typeface) {
        return new FontFamily().accept(my -> my.value = typeface);
    }

    private FontFamily() {}

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        View view = helper.getView();
        if(view instanceof TextView) {
            TextView textView = (TextView) view;
            if(!value(Typeface.class, textView::setTypeface)) {
                value(String.class, value -> textView.setTypeface(Typeface.create(value, resId)));
            }
        }
    }
}
